<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div
        class="show"
        ref="show"
        id="show"
        style="width: 100%;height:300px;"
></div>

<script src="/js/axios.js"></script>
<script src="/js/echarts.js"></script>
<script src="/js/jquery-3.4.1.js"></script>
<script>
    const request = axios.create({
        //baseURL: 'http://localhost:8800', //主机地址
        timeout: 1000, //超时时间 毫秒
        headers: {
            token: 'helen123',
        },
    })

    // 请求拦截器
    // request.interceptors.request.use(
    //     function (config) {
    //         console.log('请求拦截')
    //         console.log('config', config)
    //         config.headers.mytoken = 'helen123'
    //         //放行
    //         return config
    //     },
    //     function (error) {
    //         return Promise.reject(error)
    //     }
    // )
    //
    // // 添加响应拦截器
    // request.interceptors.response.use(
    //     function (response) {
    //         console.log('response', response)
    //         return response.data
    //     },
    //     function (error) {
    //         //对错误做点什么
    //         //......
    //         return Promise.reject(error)
    //     }
    // )


    //另一种写法
    request
        .get('/test')
        .then((response) => {
            console.log('数据获取成功', response.data)
            let data=response.data.data
            let idData = []
            let voteData = []

            for (let i in data) {
                idData.push(data[i].updateTime)
                voteData.push(data[i].data)
            }

            let e = echarts.init(document.getElementById('show'))
            let option = {
                xAxis: {
                    data: idData
                },
                yAxis: {},
                series: [
                    {
                        data: voteData,
                        type: 'line',
                        smooth: true
                    }
                ]
            }
            // 设置实例参数
            e.setOption(option)
        })
        .catch((error) => {
            console.log('数据获取失败', error)
        })
</script>

</body>
</html>